package cn.lll.vuehouse.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.dto.GoodsPageDTO;
import cn.lll.vuehouse.entity.Customer;
import cn.lll.vuehouse.entity.Provider;
import cn.lll.vuehouse.mapper.ProviderMapper;
import cn.lll.vuehouse.vo.GoodsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lll.vuehouse.entity.Goods;
import cn.lll.vuehouse.service.GoodsService;
import cn.lll.vuehouse.mapper.GoodsMapper;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author asus-pc
 * @description 针对表【bus_goods】的数据库操作Service实现
 * @createDate 2023-09-01 11:34:05
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
        implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public ResponseResult getGoodsPage(GoodsPageDTO goodsPageDTO) {
        System.out.println("goodsPageDTO = " + goodsPageDTO);
        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<Goods> lambda = new QueryWrapper<Goods>().lambda();
        lambda.like(StringUtils.isNotBlank(goodsPageDTO.getGoodsname()), Goods::getGoodsname, goodsPageDTO.getGoodsname())
                .eq(ObjectUtil.isNotEmpty(goodsPageDTO.getAvailable()), Goods::getAvailable, goodsPageDTO.getAvailable())
                .orderByDesc(Goods::getId);
        Page<Goods> goodsPage = new Page<>( goodsPageDTO.getCurrentPage(),goodsPageDTO.getLimit());
        goodsMapper.selectPage(goodsPage, lambda);
        List<Goods> records = goodsPage.getRecords();
        System.out.println("records = " + records);
        long total = goodsPage.getTotal();
        List<GoodsVO> goodsVOS = records.stream().map(goods -> {
            GoodsVO goodsVO = new GoodsVO();
            BeanUtils.copyProperties(goods, goodsVO);
            Integer providerid = goods.getProviderid();
            System.out.println("providerid = " + providerid);
            Provider provider = providerMapper.selectById(providerid);
            System.out.println("provider = " + provider);
            goodsVO.setProvidername(provider.getProvidername());
            return goodsVO;
        }).collect(Collectors.toList());
        HashMap<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("currentPageData", goodsVOS);
        responseResult.success(ResponseStatus.SUCCESS, result);
        return responseResult;
    }

    @Override
    public ResponseResult getGoodsById(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        Goods goods = goodsMapper.selectById(id);
        responseResult.success(ResponseStatus.SUCCESS, goods);
        return responseResult;
    }

    @Override
    public ResponseResult updateGoods(Goods goods) {
        ResponseResult responseResult = new ResponseResult();
        goodsMapper.updateById(goods);
        responseResult.success();
        return responseResult;
    }

    @Override
    public ResponseResult addGoods(Goods goods) {
        ResponseResult responseResult = new ResponseResult();
        // 判断商品是否存在
        String goodsname = goods.getGoodsname();
        LambdaQueryWrapper<Goods> lambda = new QueryWrapper<Goods>().lambda();
        lambda.eq(Goods::getGoodsname,goodsname);
        Goods goods1 = goodsMapper.selectOne(lambda);
        if (ObjectUtil.isNotNull(goods1)){
            responseResult.fail(ResponseStatus.GOODS_EXIST);
            return responseResult;
        }
        goodsMapper.insert(goods);
        responseResult.success();
        return responseResult;
    }
}




